/**
 *
 */
package com.geezdata.cps.ttk.api.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.geezdata.cps.base.annotation.LoggerOut;
import com.geezdata.cps.base.response.BaseResponse;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.cqrs.command.Command;
import com.geezdata.cps.cqrs.command.bus.CommandBus;
import com.geezdata.cps.ttk.common.request.CreateOrUpdateSharerRequest;
import com.geezdata.cps.ttk.common.request.QuerySharerDetailRequest;
import com.geezdata.cps.ttk.common.request.UpdateSharerAlipayRequest;
import com.geezdata.cps.ttk.common.response.CreateOrUpdateSharerResult;
import com.geezdata.cps.ttk.common.response.QuerySharerDetailResult;
import com.geezdata.cps.ttk.common.response.UpdateSharerAlipayResult;
import com.geezdata.cps.ttk.common.vo.SharerResVO;
import com.geezdata.cps.ttk.domain.domain.Sharer;
import com.geezdata.cps.ttk.query.query.SharerService;


@RestController
@RequestMapping(value="/sharer")
public class SharerController {
	@Autowired
    private CommandBus commandBus;
	@Autowired
	private Command<CreateOrUpdateSharerRequest, CreateOrUpdateSharerResult> createOrUpdateSharerCommand;
	@Autowired
	private Command<UpdateSharerAlipayRequest, UpdateSharerAlipayResult> updateSharerAlipayCommand;
	@Autowired
	private SharerService sharerService;

	@LoggerOut
    @RequestMapping(value = "/createOrUpdateSharer", method = RequestMethod.POST)
    public BaseResponse<CreateOrUpdateSharerResult> createOrUpdateSharer(@Valid @RequestBody CreateOrUpdateSharerRequest request) {
		return commandBus.dispatch(createOrUpdateSharerCommand, request);
    }
	
	@LoggerOut
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public BaseResponse<QuerySharerDetailResult> querySharerDetail(@Valid @RequestBody QuerySharerDetailRequest request) {
		QuerySharerDetailResult result = new QuerySharerDetailResult();
		Sharer sharer = sharerService.getSharerByOpenId(request.getOpenId(), Boolean.FALSE);
		result.setSharer(BeanUtil.convert(sharer, SharerResVO.class));
		return BaseResponse.success(result);
    }
	
	@LoggerOut
    @RequestMapping(value = "/updateSharerAlipay", method = RequestMethod.POST)
	public BaseResponse<UpdateSharerAlipayResult> updateSharerAlipay(@Valid @RequestBody UpdateSharerAlipayRequest request) {
		return commandBus.dispatch(updateSharerAlipayCommand, request);
	}
}
